package ua.epam.muzalevskiy.traction.configuretion;

import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

/**
 * <code>MessageConfigurer</code> configures resources that correspond to
 * user messages display
 *
 * @author Muzalevskiy Vitaliy
 */
public class MessageConfigurer {
    private static MessageConfigurer instance;
    // resourceBundle is used to get resources from the specified properties file
    private ResourceBundle resourceBundle;
    // BUNDLE_NAME specifies the name of the properties file
    private static final String BUNDLE_NAME = "massages";
    // defines the names of the apporopriate properties in the properties file
    public static final String LOGIN_ERROR_MESSAGE = "LOGIN_ERROR_MESSAGE";
    public static final String REGISTRATION_ERROR_MESSAGE = "REGISTRATION_ERROR_MESSAGE";
    public static final String SERVLET_EXCEPTION_ERROR_MESSAGE = "SERVLET_EXCEPTION_ERROR_MESSAGE";
    public static final String IO_EXCEPTION_ERROR_MESSAGE = "IO_EXCEPTION_ERROR_MESSAGE";
    public static final String NOT_ADMINISTRATOR_ERROR_MESSAGE = "NOT_ADMINISTRATOR_ERROR_MESSAGE";
    public static final String ADD_NEW_VEHICLE_ERROR_MESSAGE = "ADD_NEW_VEHICLE_ERROR_MESSAGE";
    public static final String ASSIGNE_VEHICLE_TO_ROUTE_ERROR_MESSAGE = "ASSIGNE_VEHICLE_TO_ROUTE_ERROR_MESSAGE";
    public static final String NOT_CONNECTED_VEHICLE_AND_ROUTE_ERROR_MESSAGE = "NOT_CONNECTED_VEHICLE_AND_ROUTE_ERROR_MESSAGE";
    // logger is used for logging MessageConfigurer class
    private static Logger logger = Logger.getLogger(MessageConfigurer.class);
    private static Locale currentLocale = new Locale("en");

    /**
     * Returns the only instance of the <code>MessageConfigurer</code>
     * (Singleton pattern)
     *
     * @return instance of the <code>MessageConfigurer</code>
     */
    public static MessageConfigurer getInstance() {
        if (instance == null) {
            instance = new MessageConfigurer();
            instance.resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, currentLocale);
            logger.info("Resource bundle complete");
        }
        return instance;
        }

    /**
     * Changes current locale for the ResourceBundle properties file
     * @param locale locale for the properties file
     */
    public static void ChangeLocale(Locale locale) {
        currentLocale = locale;
        getInstance().resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, currentLocale);
        logger.info("Locale changed");
    }

    /**
     * Returns appropriate String value by the specified property name
     * @param key property name
     * @return value by the specified property name
     */
    public String getProperty(String key) {
        return (String)resourceBundle.getObject(key);
    }
}
